package com.sunlee.app.api.controller;

import com.sunlee.app.api.annotation.Login;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.enums.ErrorCodeEnum;
import com.sunlee.common.service.dto.UserActionCountDTO;
import com.sunlee.common.service.dto.UserActionDetailDTO;
import com.sunlee.common.service.dto.req.UserActionDetailReqDTO;
import com.sunlee.common.service.service.UserActionService;
import com.sunlee.app.api.context.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author sunjulei
 * @date 2022/10/2
 */
@RestController
@RequestMapping("action")
public class UserActionController {

    @Autowired
    private UserActionService userActionService;

    /**
     * 埋点：浏览商品
     *
     * @param pId 商品ID
     */
    @Login
    @PostMapping("preview/{pId}")
    public Result<Boolean> preview(@PathVariable("pId") Long pId) {
        return Result.ok(userActionService.previewById(UserContext.getUserId(), pId));
    }

    /**
     * 统计用户行为数据
     */
    @Login
    @GetMapping("count")
    public Result<UserActionCountDTO> countByUserId() {
        return Result.ok(userActionService.countByUserId(UserContext.getUserId()));
    }

    /**
     * 分页查询用户行为数据
     */
    @Login
    @PostMapping("list/type")
    public Result<PageData<UserActionDetailDTO>> listByType(@RequestBody UserActionDetailReqDTO req) {
        return Result.ok(userActionService.listByType(UserContext.getUserId(), req));
    }

    /**
     * 修改行为状态
     *
     * @param id 主键ID
     * @return 是否成功
     */
    @Login
    @GetMapping("update/{id}/{status}")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Long status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            return Result.error(ErrorCodeEnum.PARAM_NOT_COMPLETE);
        }
        return Result.ok(userActionService.updateByIdAndUserId(id, UserContext.getUserId(), status));
    }
}
